$(function(){
    roleInit();
    /*选择框移动*/
    $("#add").click(function(){
        if($("#select1 option:selected").size()==0){
            show_err_msg('请选择角色！');
        }else{
            if(ajaxFun("/ajax/checkAuthority/?functionId=web_user_setPermit&deptId="))
               distributRoleRight();
            else
                show_err_msg("您目前没有给用户分配角色的权限,请联系管理员");
        }
    });
        
    $("#remove").click(function(){
        if($("#select2 option:selected").size()==0){
            show_err_msg('请选择角色！');
        }else{
            if(ajaxFun("/ajax/checkAuthority/?functionId=web_user_setPermit&deptId=")){
                var adminId = $("#adminId").val();
                var roleId = new Array();
                $("#select2 option:selected").each(function(){
                    roleId.push($(this).val());
                });
                distributRoleLeft(roleId,adminId);
            }else{
                show_err_msg("您目前没有移除用户角色的权限,请联系管理员");
            }
        }   
    }); 
        
    $("#remove_all").click(function(){
        if($("#select2 option").size()==0){
            show_err_msg('该项没有可操作的角色！');
        }else{
            if(ajaxFun("/ajax/checkAuthority/?functionId=web_user_setPermit&deptId=")){
                var adminId = $("#adminId").val();
                var roleId = new Array();
                $("#select2 option").each(function(){
                    roleId.push($(this).val());
                });
                distributRoleLeft(roleId,adminId);
            }else{
                show_err_msg("您目前没有移除用户角色的权限,请联系管理员");
            }   
        }   
    }); 
});
function distributRoleRight(){
    var adminId = $("#adminId").val();
    var roleId = new Array();
    $("#select1 option:selected").each(function(){
        roleId.push($(this).val());
    });
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj;
            try{
                obj =  eval('(' + xmlhttp.responseText + ')');
            }catch(exception){
                obj="error";
            }
            if(obj == "error"){
                show_err_msg("服务链接出错！");    
            }else{
                if(obj.status == "succeed")
                    roleInit();
                else if(obj.status == "fail")
                    show_err_msg(obj.info);
                }
            }
        }
    xmlhttp.open("POST", "/ajax/distributRoleRight/", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("adminId="+adminId+"&roleId="+roleId)
}
function distributRoleLeft(roleId,adminId){
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj;
            try{
                obj =  eval('(' + xmlhttp.responseText + ')');
            }catch(exception){
                obj="error";
            }
            if(obj == "error"){
                show_err_msg("服务链接出错！");    
            }else{
                if(obj.status == "succeed")
                    roleInit();
                else if(obj.status == "fail")
                    show_err_msg(obj.info);
                }
            }
        }
    xmlhttp.open("POST", "/ajax/distributRoleLeft/", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("adminId="+adminId+"&roleId="+roleId)

}
function roleInit(){
    var adminId = $("#adminId").val();
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj;
            try{
                obj =  eval('(' + xmlhttp.responseText + ')');
            }catch(exception){
                obj="error";
            }
            if(obj == "error"){
                show_err_msg("服务链接出错！");    
            }else{
                if(obj.status == "succeed"){
                    var outOption = $("#select1");
                    var ownOption = $("#select2");
                    outOption.html("");
                    ownOption.html("");
                    outOption.html(obj.outRole);
                    ownOption.html(obj.ownRole);
                    $("#userName").val(obj.userName);
                }else if(obj.status == "fail")
                    show_err_msg(obj.info);
                }
            }
        }
    xmlhttp.open("POST", "/ajax/distributRoleInit/", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("adminId="+adminId)
}